//
//  DetailViewController.swift
//  CodingTest
//
//  Created by LYPC on 2021/11/18.
//

import UIKit

struct DetailRouterParam: RouterParam {
    var model: DataModel
}

extension DetailViewController: Routable {
    static func initWithRouter(params: RouterParam?) -> UIViewController? {
        guard let data = params as? DetailRouterParam
        else { k_log("🔴 error init \(self) by illegal params"); return nil }
        return DetailViewController(initData: data)
    }
}

class DetailViewController: BaseViewController {

    private let viewModel = DetailViewModel()
    private let tableView = CTRefreshTableView()

    private let initData: RouterParam
    init(initData: DetailRouterParam) {
        self.initData = initData
        viewModel.model = initData.model
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupLayout()
        setupStyle()
        setupReactive()
        // Do any additional setup after loading the view.
    }
}

// MARK: - Reactive
extension DetailViewController {
    
    func setupReactive() {
        tableView.reloadData()
    }
}

// MARK: - ********* tableView UITableViewDataSource、UITableViewDelegate
extension DetailViewController: UITableViewDataSource, UITableViewDelegate {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return viewModel.getNumberOfRows()
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        return viewModel.getHeightForRow(indexPath)
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.row == 0 {
            let cell = DetailContentCell.cell(withTableView: tableView, indexPath: indexPath)
            cell.configure(viewModel.model)
            return cell
        } else {
            let cell = DetailImageCell.cell(withTableView: tableView, indexPath: indexPath)
            cell.configure(viewModel.model)
            cell.imgAction = { [weak self] idx in
                        guard let self = self else { return }
                        self.viewModel.showImg(tableView: tableView, indexPath: indexPath, currentIdx: idx)
            }
            return cell
        }
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
    }
}

// MARK: - UI Setup
private extension DetailViewController {
    
    func setupLayout() {
        view.addSubview(tableView)
        tableView.snp.makeConstraints { (m) in
            m.top.equalToSuperview().inset(kNavBtm)
            m.leading.trailing.bottom.equalToSuperview()
        }
    }
    
    func setupStyle() {
        navBar.title = "数据详情"
        tableView.do { (v) in
            v.tableHeaderView = UIView()
            v.tableFooterView = UIView()
            v.backgroundColor = .color(.white)
            v.delegate = self
            v.dataSource = self
            v.keyboardDismissMode = .onDrag
            v.register(DetailContentCell.self,
                       forCellReuseIdentifier: DetailContentCell.reuseID)
            v.register(DetailImageCell.self,
                       forCellReuseIdentifier: DetailImageCell.reuseID)
        }
        tableView.addError()
    }
}

